define(function(require, exports){
	var $ = require('jquery');
	var pubjs = require('pubjs');
	var view  = require('@base/view');

	// 创意包上传子模块
	var SubCreative = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'fileId': 0,
				'data': null,
				'class': 'uk-form M-uploaderCreative',
				'defaultImage': window.ROOT('resources/images/default.png')
			});
			this.$data = null;
			this.$fileId = config.get('fileId');
			this.Super('init', arguments);
		},
		afterBuild: function(){
			var c = this.getConfig();
			var el = this.getDOM();
			$([
				'<li class="" data-id=' + c.fileId +'>',
					'<div class="M-uploaderCreativeLayoutColumn M-uploaderCreativeThumb"><img alt="'+LANG('缩略图')+'" src="'+c.defaultImage+'"/></div>',
					'<div class="M-uploaderCreativeLayoutColumn">',
						'<span pub-name="name" pub-mod="@base/common/input.text" class="M-uploaderCreativeName"/>',
						'<div class="M-uploaderCreativeInfo">',
							'<label>'+LANG('类型：')+'</label><span class="M-uploaderCreativeType mr20" />',
							'<label>'+LANG('大小：')+'</label><span class="M-uploaderCreativeSize" />',
						'</div>',
						'<div><label>'+LANG('尺寸：')+'</label><span class="M-uploaderCreativeShape"/></div>',
					'</div>',
					'<div class="M-uploaderCreativeLayoutColumn status" />',
					'<div class="M-uploaderCreativeLayoutColumn" pub-name="assist" pub-mod="modules/upload.assist" />',
					'</div>',
				'</li>'
			].join('')).appendTo(el);

			this.$doms = {
				type: el.find('.M-uploaderCreativeType'),
				size: el.find('.M-uploaderCreativeSize'),
				shape: el.find('.M-uploaderCreativeShape')
			};

			var config = {
				assist: {
					hasDelete: true,
					hasPreview: false,
					target: el.find('.status'),
					type: 'category'
				},
				name: {
					value: '',
					width: 300,
					events: 'blur'
				}
			};

			this.buildFromTemplate(config);
		},
		afterBuildTemplate: function(){
			var data = this.getConfig('data');
			if(data){
				this.setData(data);
				this.$.assist.update(true);
			}
		},
		onInputBlur: function(ev){
			if(this.$data){
				this.$data.name = ev.param.value;
			}
			return false;
		},
		onFileUploaded: function(ev){
			var file = ev.param[0];
			var data = ev.param[1];

			if(this.$fileId == file.id){
				this.$.assist.update(true);
				this.setData(data, true);
				this.fire('uploadSuccess', file.id);
			}
		},
		//todo 尝试将isEdit去掉
		setData: function(data, isEdit){
			var doms = this.$doms;

			if(isEdit){
				this.$.assist.update(true);
			}
			var id = data.id;
			data.id = isEdit ? 0 : id;
			data.fileId = isEdit ? id : 0;

			this.$data = data;

			this.$.name.setValue(data.name||'');
			doms.type.text(data.type||'');
			doms.size.text(data.size||'');
			doms.shape.text((data.width||'')+'*'+(data.height||''));

			// 设置预览地址
			this.$.assist.setData({id: (isEdit ? 'fid_'+data.fileId : data.id)});
		},
		getValue: function(){
			var data = this.$data;
			return {
				name: data.name,
				creativeId: data.id,
				fileId: data.fileId,
				width: data.width,
				height: data.height,
				type: 'upload'
			};
		},
		onChildDeleted: function(){
			this.destroy();
		}
	});
	exports.base = SubCreative;

});